package ui.activity.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import com.dispatchapp.andlei.R;
import netWork.NetUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 提现界面
 * @author Andlei
 * @DATE 2019/1/22
 */
public class CashWithdrawalActivity extends BaseLayoutActivity {
    private TextView tv_yue_e,tv_all_tixian;
    private EditText ed_money;
    private ImageView img_apply_tixian;
    private Double money_double = 0.0;
    private NetUtils netUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("提现",true);
        setTitleBg(R.color.color_333333);
        setTitlefontBg();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash_withdrawal;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity,netRequestCallBack);
        tv_yue_e =  getView(R.id.tv_yue_e);
        tv_all_tixian = getView(R.id.tv_all_tixian);
        ed_money = getView(R.id.ed_money);
        img_apply_tixian = getView(R.id.img_apply_tixian);
    }

    @Override
    protected void formatViews() {
        ed_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    ed_money.setTextColor(Color.parseColor("#00B89D"));
                    img_apply_tixian.setImageResource(R.mipmap.apply_green);
                    img_apply_tixian.setEnabled(true);
                }else {
                    ed_money.setTextColor(Color.parseColor("#9B9B9B"));
                    img_apply_tixian.setImageResource(R.mipmap.apply_gray);
                    img_apply_tixian.setEnabled(false);
                }
            }
        });
        tv_all_tixian.setOnClickListener(this);
        img_apply_tixian.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        tv_yue_e.setText("可转出的余额"+money_double+"元");
    }

    @Override
    protected void getBundle(Bundle bundle) {
        if(!TextUtils.isEmpty(bundle.getString("balance"))){
            money_double = Double.parseDouble(bundle.getString("balance"));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_all_tixian:
                Map<String,Object> map = new HashMap<>();
                map.put("money",""+money_double);
                ed_money.setText(""+money_double);
//                if(money_double<=0){
//                    toast("申请金额不能小于0元");
//                    return;
//                }
//                if(Double.parseDouble(ed_money.getText().toString().trim())<=0){
//                    toast("申请金额不能小于0元");
//                    return;
//                }
//                netUtils.post("user/cash-out",map,BaseBean.class,false);
                break;
            case R.id.img_apply_tixian:
                Map<String,Object> map1 = new HashMap<>();
                map1.put("money",""+ed_money.getText());
                if(TextUtils.isEmpty(ed_money.getText().toString().trim())){
                    return;
                }
                if(Double.parseDouble(ed_money.getText().toString().trim())>money_double){
                    toast("申请金额超出余额");
                    return;
                }
                if(Double.parseDouble(ed_money.getText().toString().trim())<=0){
                    toast("申请金额不能小于0元");
                    return;
                }
                netUtils.post("rider/user/cash-out",map1,BaseBean.class,false);
                break;
        }
    }
    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            if(t.getCode() == 200){
                toast("提现成功");
                finish();
            }else {
                toast(t.getMsg());
            }
        }
    };
}
